﻿using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using Microsoft.SharePoint;
using SPQueryToSql.Extensions;

namespace SPQueryToSql.ApplicationObjects.CAML
{
    internal class Membership : BaseOperator
    {
        public string Type;

        public Membership(XElement element, IEnumerable<FieldInfo> listFields)
            : base(element, listFields)
        {
            Type = element.AttributeValueString("Type", string.Empty);
            var frEl = element.Element(SPQueryToSql.CAML.FieldRef);
            if (frEl != null)
                FieldRef = new FieldRef(frEl);
        }

        public override string CAMLElementName
        {
            get { return "Membership"; }
        }

        public override string ToSql(Query query)
        {
            var sql = new StringBuilder();
            switch (Type.ToUpper())
            {
                case "SPWEB.ALLUSERS":
                    sql.AppendFormat("EXISTS (SELECT TOP 1 1 FROM dbo.UserInfo WHERE CONVERT(NVARCHAR(10), tp_ID) + ';#' + tp_Title + ';#' = ResultTable.[{0}] AND tp_SiteID = '{1}' AND tp_IsActive = 1)",
                FieldInfo.InternalName, FieldInfo.SiteId.ToString("B"));
                    break;
                case "SPGROUP": break;
                case "SPWEB.GROUPS": break;
                case "SPWEB.USERS": break;
                default: //"CURRENTUSERGROUPS"
                    break;
            }
            sql.AppendFormat("EXISTS (SELECT TOP 1 1 FROM dbo.UserInfo WHERE CONVERT(NVARCHAR(10), tp_ID) + ';#' + tp_Title + ';#' = ResultTable.[{0}] AND tp_SiteID = '{1}' AND tp_IsActive = 1)", 
                FieldInfo.InternalName, FieldInfo.SiteId.ToString("B"));
            switch (FieldInfo.Type)
            {
                case SPFieldType.Invalid:
                case SPFieldType.AllDayEvent:
                    break;
                case SPFieldType.Text:
                    
                    break;
                case SPFieldType.Note:
                    sql.AppendFormat("[{0}] >= '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.DateTime:
                    sql.AppendFormat("[{0}] >= '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Choice:
                    sql.AppendFormat("[{0}] >= '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.URL:
                    sql.AppendFormat("[{0}] >= '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Integer:
                    sql.AppendFormat("[{0}] >= {1}", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Counter:
                    sql.AppendFormat("[{0}] >= {1}", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Number:
                    sql.AppendFormat("[{0}] >= {1}", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Currency:
                    sql.AppendFormat("[{0}] >= {1}", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.MaxItems:
                    sql.AppendFormat("[{0}] >= {1}", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Computed:
                    sql.AppendFormat("[{0}] >= '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                case SPFieldType.Calculated:
                    sql.AppendFormat("[{0}] >= '{1}'", FieldInfo.InternalName, Value.Text);
                    break;
                default:
                    sql.Append("1=1");
                    break;
            }
            return sql.ToString();
        }
    }
}
